﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebService.DAO;
using WebService.VO;

namespace WebService.BUS
{
    public class UserRatingBUS
    {
        UserRatingDAO userRatingDAO;
        public UserRatingBUS()
        {
            userRatingDAO = new UserRatingDAO();
        }

        /// <summary>
        /// Get user rating
        /// </summary>
        /// <param name="userRating"></param>
        /// <returns></returns>
        public int getUserRating(UserRatingVO userRating)
        {
            return userRatingDAO.getUserRating(userRating);
        }

        /// <summary>
        /// Insert user rating
        /// </summary>
        /// <param name="userRating"></param>
        public void insertRating(UserRatingVO userRating)
        {
            userRatingDAO.insertRating(userRating);
        }

        /// <summary>
        /// update user rating
        /// </summary>
        /// <param name="userRating"></param>
        public void updateRating(UserRatingVO userRating)
        {
            //drateClient parameter
            double drateClient = 0;
            //oldRate parameter
            double oldRate = 0;
            //newRate parameter
            double newRate = 0;
            //counrRate parameter
            int CountRate = 0;
            ItemDAO itemDAO = new ItemDAO();
            ItemVO item = itemDAO.getItem(userRating.Item.ItemId.ToString(), "vi");
            if (item != null)
            {
                //set new value for oldRate is value of column Rate in Database
                oldRate = item.Rate;
                // set new values for countRate is value of column CountRate in Database
                CountRate = item.CountRate;
            }
            if (userRatingDAO.checkUserRatingExist(userRating))
            {
                int oldRating = userRatingDAO.getUserRating(userRating);
                drateClient = Convert.ToDouble(userRating.Rating);
                if(oldRating!=drateClient)
                {
                    //set new values for newRate variable
                    newRate = (oldRate * CountRate - oldRating + drateClient) / (CountRate);

                    //update new rating of item
                    itemDAO.updateRating(newRate, CountRate, userRating.Item.ItemId.ToString());
                    //update new rating of user for item
                    userRatingDAO.updateRating(userRating);
                }
            }
            else 
            {
                drateClient = Convert.ToDouble(userRating.Rating);
                //set new values for newRate variable
                newRate = (oldRate * CountRate + drateClient) / (CountRate + 1);
                //add CountRate = CountRate + 1
                CountRate = CountRate + 1;

                itemDAO.updateRating(newRate, CountRate, userRating.Item.ItemId.ToString());
                //inser user rating
                userRatingDAO.insertRating(userRating);
            }
        }
    }
}